# -*- coding: utf-8 -*-
#menu class

import pyglet
from pyglet import event, graphics
from pyglet import text, resource
from pyglet.window import key

import camera

HIGHLIGHT = (220, 200, 70, 255)
#LOWLIGHT = (150, 110, 40, 255)
LOWLIGHT = (0, 0, 0, 255)
try:
    resource.add_font("SMonohand.ttf")
except resource.ResourceNotFoundException:
    print "Warning! Font not found!"
FONT_NAME = "SMonohand"
FONT_SIZE = 20

class MenuItem(text.Label):
    def __init__(self, text, font_name=None, font_size=None, 
       high_color=(255,255,255,255), low_color=(0,0,0,0), x=0, y=0,
       batch=None):
        self.high_color = high_color
        self.low_color = low_color
        super(MenuItem, self).__init__(text, font_name, font_size, 
           False, False, low_color, x, y, None, None, "center", "top",
           "center", False, None, batch, None)

    def highlight(self):
        self.color = self.high_color
        self.bold = True

    def lowlight(self):
        self.color = self.low_color
        self.bold = False
       

class Menu(event.EventDispatcher, graphics.Batch):
    def __init__(self, parent, x, y, items):
        super(Menu, self).__init__()
        self.parent = parent
        self.x, self.y = x, y

        self.width = 0
        self.items = []
        y = y #you never know...
        for txt in items:
            ob = MenuItem(txt, FONT_NAME, FONT_SIZE, HIGHLIGHT, 
               LOWLIGHT, self.x, y, self)
            self.items.append(ob)
            y -= ob.content_height
            self.width = max(self.width, ob.content_width)
        self.height = self.y - y
        
        self.selected = 0
        if self.items:
            self.items[self.selected].highlight()
        self.parent.push_handlers(self)
        self.register_event_type('on_select')

    def on_key_press(self, symbol, modifier):
        if symbol == key.UP:
            self.up()
        elif symbol == key.DOWN:
            self.down()
        elif symbol == key.ENTER or key == key.SPACE:
            self.select()
        elif symbol == key.ESCAPE:
            pyglet.app.exit()
        return event.EVENT_HANDLED

    def up(self):
        if self.items:
            self.items[self.selected].lowlight()
            self.selected -= 1
            self.selected %= len(self.items)
            self.items[self.selected].highlight()

    def down(self):
        if self.items:
            self.items[self.selected].lowlight()
            self.selected += 1
            self.selected %= len(self.items)
            self.items[self.selected].highlight()

    def select(self):
        self.dispatch_event('on_select', self.items[self.selected].text)
        #self.parent.remove_handlers(self)

    #def on_close(self, value):
        #return event.EVENT_HANDLED

    def __del__(self):
        self.parent.remove_handlers(self)

#if __name__ == "__main__":
    #win = pyglet.window.Window()
    #m = Menu(win, win.width/2, win.height, ["bolle", "kalle", "gurra"])
    #@m.event
    #def on_close(op):
        #print op

    #@win.event
    #def on_draw():
        #win.clear()
        #m.draw()
    #pyglet.app.run()